package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    private  ObjectMapper objectMapper = new ObjectMapper();
    private StudentServiceImpl studentService = new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String method = req.getParameter("method");
        if ("selectByPage".equals(method)){
            selectByPage(req,resp);
        }else if ("addStudent".equals(method)){
            addStudent(req,resp);
        }else if ("updateStudent".equals(method)){
            updateStudent(req,resp);
        }else if ("deleteStudent".equals(method)){
            deleteStudent(req,resp);
        }
    }

    private void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1 获取要删除的学的number
        String number = req.getParameter("number");
        //2 调用service删除学生信息
        int i = studentService.deleteStudent(number);
        //3 响应结果到客户端
        resp.getWriter().write(i+"");
    }

    private void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student = new Student();
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(student,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int i = studentService.updateStudent(student);
        resp.getWriter().write(i+"");
    }

    private void addStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student = new Student();
        try {
            BeanUtils.populate(student,req.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int i = studentService.addStudent(student);
        resp.getWriter().write(i+"");

    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1 获取请求参数，currentPage、pageSize
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //2 调用service层方法获取结果PageInfo对象
        PageInfo<Student> pageInfo = studentService.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //3 转换成json响应给客户端
        objectMapper.writeValue(resp.getWriter(),pageInfo);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
